package xin.nick.demo;

/**
 * @Auther: Nick inickning@outlook.com
 * @Date: 2021/3/18 19:36
 * @Description:
 */
public class Test01 {

    public static int currentThread = 1;
    public static int count = 1;


    public static void main(String[] args) {

        Object o = new Object();

        MyThread2 myThread1 = new MyThread2();
        myThread1.setNumber(1);
        myThread1.setNextNumber(2);
        MyThread2 myThread2 = new MyThread2();
        myThread2.setNumber(2);
        myThread2.setNextNumber(3);
        MyThread2 myThread3 = new MyThread2();
        myThread3.setNumber(3);
        myThread3.setNextNumber(1);

        myThread1.start();
        myThread2.start();
        myThread3.start();
    }

}

class MyThread2 extends Thread {

    public static final int MAX_NUMBER = 36;
    public static final int RUN_COUNT = 3;

    private int number = 0;
    private int nextNumber = 0;

    public void setNumber(int number) {
        this.number = number;
    }

    public void setNextNumber(int nextNumber) {
        this.nextNumber = nextNumber;
    }
    @Override
    public void run() {
        while (true) {
            synchronized (Object.class) {
                if (Test01.count < MAX_NUMBER) {
                    if (Test01.currentThread == number) {
                        for (int i = 0; i < RUN_COUNT; i++) {
                            try {
                                Thread.sleep(Math.round(500));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            String myName = "线程" + number;
                            System.out.println(myName + ": " + Test01.count++);
                        }
                        Test01.currentThread = nextNumber;
                    }
                } else {
                    break;
                }
            }
        }

    }
}
